<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="1"></el-col>
      <el-col :span="12" :xs="23">
        <el-form
          class="login_form"
          :model="loginFormData"
          :rules="loginRules"
          ref="loginForm"
        >
          <h1>Hello</h1>
          <h2>欢迎来到王某人的工程</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginFormData.username"
              type="text"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="loginFormData.password"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" @click="onLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reqLogin } from '@/api/user'
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import useUserStore from '@/store/modules/user'
import router from '@/router'
import { ElNotification } from 'element-plus'
import { LoginParams } from '@/types/login'
import time from '@/utils/time'
import type { FormInstance, FormRules } from 'element-plus'

// 设置表单实例
const loginForm = ref<FormInstance>()

// 自定义表单验证
const validateUsername = (_rule: any, value: string, callback: any) => {
  if (!value) {
    return callback(new Error('用户名不能为空哦'))
  } else {
    if (/^\w{5,10}$/.test(value)) {
      callback()
    } else {
      return callback(new Error('用户名要是长度在5-12之间，的数字字母下划线~'))
    }
  }
}
const validatePassword = (_rule: any, value: string, callback: any) => {
  if (!value) {
    return callback(new Error('用户名不能为空哦'))
  } else {
    if (/^\w{5,10}$/.test(value)) {
      callback()
    } else {
      return callback(new Error('密码要是长度在5-12之间，的数字字母下划线~'))
    }
  }
}

// 表单的校验
const loginRules = ref<FormRules<LoginParams>>({
  username: [
    { validator: validateUsername, trigger: 'blur' },
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    // { min: 5, max: 24, message: '用户名长度在5-12之间', trigger: 'change' },
  ],
  password: [
    { validator: validatePassword, trigger: 'blur' },
    // { required: true, message: '密码不能为空', trigger: 'blur' },
    // { min: 6, max: 24, message: '密码长度在6-12之间', trigger: 'change' },
  ],
})

// 收集账号和密码的数据
const loginFormData = ref<LoginParams>({
  username: 'admin',
  password: '111111',
})

// 引入用户信息
const userStore = useUserStore()
// 登录按钮的回调
const onLogin = async () => {
  await loginForm.value!.validate()
  const res = await reqLogin({
    username: loginFormData.value.username,
    password: loginFormData.value.password,
  })
  if (res.code === 200) {
    // 检查res.data是否包含token属性
    userStore.setProfileToken(res.data.token!)
    router.push('/')
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${time()}好~`,
    })
  } else {
    ElNotification({
      type: 'error',
      message: res.data.message,
    })
  }
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat center/ cover;
  .login_form {
    position: relative;
    width: 70%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat center/ cover;
    padding: 40px;
    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
